﻿using _2._18._2015_MoviesInClass.Data;
using _2._18._2015_MoviesInClass.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using _2._18._2015_MoviesInClass.Data.Models;
using _2._18._2015_MoviesInClass.Adapters;

namespace _2._18._2015_MoviesInClass.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private IMovieAdapter _adapter;

        //public HomeController()
        //{
        //    _adapter = new MovieFBAdapter();
        //}

        public HomeController(IMovieAdapter adapter)
        {
            _adapter = adapter;
        }

        public ActionResult Index()
        {
            string userId = User.Identity.GetUserId();

            MovieListViewModel model = new MovieListViewModel();

            model.Movies = _adapter.GetMovies(userId);
            

            //model.UserName = User.Identity.GetUserName();
            //model.AverageRating = model.Movies.Average(m => m.Rating);
            
            return View(model);
        }

        public ActionResult Create()
        {
            MovieViewModel model = new MovieViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(MovieViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string userId = User.Identity.GetUserId();

            _adapter.CreateMovie(model, userId);

            return RedirectToAction("Index");
        }
    }
}